//
//  JCReportModel.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/30.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCReportModel.h"
#import "NSString+Extension.h"

@interface JCReportModel ()

@end

@implementation JCReportModel

+ (JCReportModel *)creatReportDetailModelWith:(NSDictionary *)sourceDic{
    JCReportModel *weekModel = [JCReportModel new];
    weekModel.dailyModels = [NSMutableArray array];
    for (NSInteger i = 0; i < 7; i++) {
        JCReportListModel *clearModel = [JCReportListModel creatClearModel];
        clearModel.week = @(i).stringValue;
        [weekModel.dailyModels addObject:clearModel];
    }
    for (NSDictionary *dailyDic in sourceDic[@"WeekData"]) {
        if (dailyDic.allKeys.count > 0) {
            JCReportListModel *dailyModel = [JCReportListModel creatDailyModelWithDic:dailyDic];
            [weekModel.dailyModels replaceObjectAtIndex:dailyModel.week.integerValue withObject:dailyModel];
        }
    }

    weekModel.weekTotalModel = [JCReportWeekTotalModel creatReportTotalModelWith:sourceDic[@"WeekTotal"]];
    weekModel.lastWeekTotalModel = [JCReportWeekTotalModel creatReportTotalModelWith:sourceDic[@"LastWeekTotal"]];
    return weekModel;
}

@end







/**
 运动报告详细数据
 */
@interface JCReportWeekTotalModel ()

@end

@implementation JCReportWeekTotalModel

+ (JCReportWeekTotalModel *)creatReportTotalModelWith:(NSDictionary *)sourceDic{
    JCReportWeekTotalModel *model = [JCReportWeekTotalModel new];
    model.duration = [NSString checkIfNullWithString:sourceDic[@"Duration"]];
    model.swings = [NSString checkIfNullWithString:sourceDic[@"Swings"]];
    model.caroline = [NSString checkIfNullWithString:sourceDic[@"Caroline"]];
    model.avgDuration = [NSString checkIfNullWithString:sourceDic[@"AvgDuration"]];
    model.avgSwings = [NSString checkIfNullWithString:sourceDic[@"AvgSwings"]];
    model.avgCaroline = [NSString checkIfNullWithString:sourceDic[@"AvgCaroline"]];
    return model;
}

@end








@interface JCReportListModel ()

@end

@implementation JCReportListModel

+ (NSArray <JCReportListModel *> *)creatReportListWithSource:(NSDictionary *)sourceDic{
    NSMutableArray *allReports = [NSMutableArray array];
    for (NSString *year in sourceDic.allKeys) {
        NSArray *yearWeekArray = sourceDic[year];
        for (NSArray *weekArray in yearWeekArray) {
            JCReportListModel *weekModel = [JCReportListModel new];
            weekModel.dailyModels = [NSMutableArray array];
            for (NSInteger i = 0; i < 7; i++) {
                JCReportListModel *clearModel = [JCReportListModel creatClearModel];
                clearModel.week = @(i).stringValue;
                [weekModel.dailyModels addObject:clearModel];
            }
            for (NSDictionary *dailyDic in weekArray) {
                JCReportListModel *dailyModel = [JCReportListModel creatDailyModelWithDic:dailyDic];
                [weekModel.dailyModels replaceObjectAtIndex:dailyModel.week.integerValue withObject:dailyModel];
                weekModel.year = dailyModel.year;
                weekModel.yearWeek = dailyModel.yearWeek;
            }
            [allReports addObject:weekModel];
        }
    }
    return allReports.mutableCopy;
}

+ (JCReportListModel *)creatDailyModelWithDic:(NSDictionary *)sourceDic{
    JCReportListModel *dailyModel = [JCReportListModel new];
    dailyModel.year = [NSString checkIfNullWithString:sourceDic[@"Year"]];
    dailyModel.yearWeek = [NSString checkIfNullWithString:sourceDic[@"YearWeek"]];
    dailyModel.week = [NSString checkIfNullWithString:sourceDic[@"Week"]];
    dailyModel.date = [NSString checkIfNullWithString:sourceDic[@"Date"]];
    dailyModel.duration = [NSString checkIfNullWithString:sourceDic[@"Duration"]];
    dailyModel.maxSpeed = [NSString checkIfNullWithString:sourceDic[@"MaxSpeed"]];
    dailyModel.swings = [NSString checkIfNullWithString:sourceDic[@"Swings"]];
    dailyModel.caroline = [NSString checkIfNullWithString:sourceDic[@"Caroline"]];
    return dailyModel;
}

+ (JCReportListModel *)creatClearModel{
    JCReportListModel *model = [JCReportListModel new];
    model.duration = @"0";
    model.swings = @"0";
    model.caroline = @"0";
    model.maxSpeed = @"0";
    
//    model.duration = @(arc4random()%1000).stringValue;
//    model.swings = @(arc4random()%1000).stringValue;
//    model.caroline = @(arc4random()%1000).stringValue;
//    model.maxSpeed = @(arc4random()%1000).stringValue;
    return model;
}

@end
